﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZaidimasSandelys.Models.DataStructures;
using ZaidimasSandelys.Models.EntityModel;
using ZaidimasSandelys.Models.Interfaces;

namespace ZaidimasSandelys.Models.Classes
{
    public class AppSqlCeDataEF : IAppData
    {
        private ZaidimasSandelysEntities _context;

        public AppSqlCeDataEF(string connString = null)
        {
            if (String.IsNullOrEmpty(connString))
                this._context = new ZaidimasSandelysEntities();
            else
                this._context = new ZaidimasSandelysEntities(connString);
        }

        public static GameLevel SafeConvertionToGameLevel(short value)
        {
            try
            {
                return (GameLevel)value;
            }
            catch (Exception)
            {
                return GameLevel.Unassigned;
            }
        }

        #region Interfeiso metodai

        public IEnumerable<DataStructures.MainMenuItem> GetMainMenu()
        {
            var query = 
                from mn in _context.Menus
                where mn.Code == "Main"
                orderby mn.Position
                select new {
                                Text = mn.Text,
                                Value = mn.Value
                           };
            
            var rzlt = new List<MainMenuItem>();
            foreach (var z in query)
            {
                rzlt.Add(new MainMenuItem()
                                {
                                    Text = z.Text,
                                    Value = SafeConvertionToGameLevel(z.Value)
                                });
            }
            return rzlt;
        }

        public string[] GetStartButtonText()
        {
            var query = (
                    from mn in _context.Menus
                    where mn.Code == "bStart"
                    orderby mn.Position
                    select mn.Text
                ).ToArray();

            return query;
        }

        public IEnumerable<DataStructures.VariantMenuItem> GetGameVariants(DataStructures.GameLevel gameComplexity)
        {
            short complexity = (short)gameComplexity;
            var query = (
                from mn in _context.Menus
                where mn.Code == "gmVrnt" && mn.Value == complexity
                select new {
                    Text = mn.Text,
                    Value = mn.Position
                }).ToList();

            return query.ConvertAll(z => new DataStructures.VariantMenuItem()
                        {
                            Text = z.Text,
                            Value = z.Value
                        });
        }

        public string[] GetMultilanguageConstants(string id)
        {
            return (
                    from mlt in _context.Multilanguages
                    where mlt.Code == id
                    orderby mlt.Position
                    select mlt.Text
                ).ToArray();
        }






        public DataStructures.GameHeader LoadGameHeader(short level, short variant)
        {
            var query = (
                from hd in _context.GameHeaders
                where hd.Level == level && hd.Variant == variant
                select new
                {
                    Id = hd.id,
                    Level = hd.Level,
                    Variant = hd.Variant,
                    PersonX = hd.PersonX,
                    PersonY = hd.PersonY,
                }).SingleOrDefault();

            if (query == null)
                return null;
            else
                return new DataStructures.GameHeader()
                            {
                                Id = query.Id,
                                Level = query.Level,
                                Variant = query.Variant,
                                Person = new System.Windows.Point(query.PersonX, query.PersonY),
                                BestResult = 0
                            };
        }

        public IEnumerable<IGameElement> LoadVisualElements(int gameId, CustomDrawTools.VisualType elementType)
        {
            var rzlt = new List<IGameElement>();
            lock (this)
            {
                var sGameType = (short)elementType;
                var query =
                    from el in _context.GameElements
                    where el.GameId == gameId && el.ElementType == sGameType
                    select new
                    {
                        GameId = el.GameId,
                        ElementType = el.ElementType,
                        Xmin = el.PointX,
                        Xmax = el.ToPointX,
                        Ymin = el.PointY,
                        Ymax = el.ToPointY,
                    };
                foreach (var range in query)
                {
                    short xMax = range.Xmax ?? range.Xmin;
                    short yMax = range.Ymax ?? range.Ymin;
                    short xMin = range.Xmin, yMin = range.Ymin;

                    if (xMin > xMax)  // Sukeisti vietom
                    {
                        xMin = xMax;
                        xMax = range.Xmin;
                    }
                    if (yMin > yMax) // Sukeisti vietom
                    {
                        yMin = yMax;
                        yMax = range.Ymin;
                    }

                    for (short x = xMin; x <= xMax; x++)
                        for (short y = yMin; y <= yMax; y++)
                        {
                            rzlt.Add(new GameElement(x, y, range.ElementType));
                        }
                }
            }

            return rzlt;
        }


        public System.Data.Common.DbConnection Conn
        {
            get { throw new NotImplementedException(); }
        }
        public void CloseConnection()
        {
            throw new NotImplementedException();
        }
        #endregion
    }
}
